package com.he1618;

import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
import com.influxdb.query.FluxRecord;
import com.influxdb.query.FluxTable;
import com.influxdb.query.dsl.Flux;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.List;

public class QueryApp {

    public static void main(String[] args) {

        System.out.println( "Hello World!" );
        String token = "J_b9Esev3GqZtbFLiA_bo6rvye7aaxssC8MiO99KtXgQmfd-VZyNKo3LvoMWfIfNiEZyLncr1UObY2YrArlTbg==";
        String bucket = "ada";
        String org = "ada";

        InfluxDBClient client = InfluxDBClientFactory.create("http://192.168.3.108:8086", token.toCharArray());
        String query = "from(bucket: \"ada\") |> range(start: -10s)";
        List<FluxTable> tables = client.getQueryApi().query(query, org);

        for (FluxTable table : tables) {
            for (FluxRecord record : table.getRecords()) {
                System.out.println(record.getValue());
            }
        }

        Long time=System.currentTimeMillis()/1000-60*60*2;

        Flux flux = Flux
                .from("ada").range(time)
                //.window(15L, ChronoUnit.MINUTES, 20L, ChronoUnit.SECONDS)
                //.sum();
        ;

        System.out.println(flux.toString());

        client.getQueryApi().query(flux.toString(), org,(a,b)->{
            System.out.println(b.getValue());
        });
    }
}
